package com.bw.live.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bw.live.R
import com.bw.live.databinding.ItemChatContentBinding
import com.bw.live.model.entity.ChatEntity

/**
 * @author:created by hahajing
 * 时间：2024/5/9 0009
 * 企鹅号:444511958
 * 描述:
 */
class ChatAdapter:RecyclerView.Adapter<ChatAdapter.ViewHolder>() {
    private var chats:MutableList<ChatEntity> = mutableListOf()
    private lateinit var mContext:Context

    fun refresh(_chats:MutableList<ChatEntity>){
        chats=_chats
        notifyDataSetChanged()
    }

    fun appendItem(entity: ChatEntity){
        chats.add(0,entity)
        notifyItemInserted(0)
        mRecyclerView.scrollToPosition(0)
    }

    fun appendItems(_chats: MutableList<ChatEntity>){
        chats.addAll(0,_chats)
        notifyItemRangeInserted(0,_chats.size)
        mRecyclerView.scrollToPosition(0)
    }

    private lateinit var mRecyclerView:RecyclerView
    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        mRecyclerView=recyclerView
    }


    inner class ViewHolder(view:View):RecyclerView.ViewHolder(view){
        val binding:ItemChatContentBinding by lazy {
            ItemChatContentBinding.bind(view)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        mContext = parent.context
        val view  = LayoutInflater.from(mContext).inflate(R.layout.item_chat_content,null,false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return chats.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val entity = chats.get(position)
        holder.binding.tvLiveItemchatcontentUser.text = entity.username
        holder.binding.tvLiveItemchatcontentContent.text=entity.content
    }
}